#include "HelpInfoManager.h"

#include "../TinyXML/tinyxml.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"

HelpInfoManager* HelpInfoManager::pInstance = nullptr;
HelpInfoManager::HelpInfoManager(void)
{
	infoVector.reserve(1);
	objectVector.reserve(1);
}


HelpInfoManager::~HelpInfoManager(void)
{
	clearObjects();
	clearHelpInfo();
}


HelpInfoManager* HelpInfoManager::GetInstance( void )
{
	// Allocate the static pointer if necessary
	if( pInstance == nullptr )
		pInstance = new HelpInfoManager;

	// Return the singleton
	return pInstance;
}
void HelpInfoManager::DeleteInstance( void )
{
	delete pInstance;
	pInstance = nullptr;
}



void HelpInfoManager::clearHelpInfo()
{
	for(unsigned int i =0; i < infoVector.size(); ++i)
	{
		if(infoVector[i]->getImageID() != -1)
		{
			CSGD_TextureManager::GetInstance()->UnloadTexture(infoVector[i]->getImageID());
		}
		delete infoVector[i];
	}
	infoVector.clear();
}

//Loads in associated object names, descriptions, and images from an XML file.
//All images should be loaded from the same folder as the XML files.
//The isActive
void HelpInfoManager::loadHelpInfo()
{
	//for(int i = 0; i < 10; ++i)
	//{
	//	HelpInfo* tempInfo = new HelpInfo();
	//	infoVector.push_back(tempInfo);
	//}
	TiXmlDocument doc;
	doc.LoadFile( "XML/help/helpInfo.xml" );

	TiXmlElement* root = doc.RootElement();
	if(root	!= nullptr)
	{
		//Check for object
		 if(root->NoChildren())
		 {
			assert("helpInfo.xml has no children!");
		 }
		for(TiXmlElement* objectEl = root->FirstChildElement("object"); objectEl != NULL; objectEl = objectEl->NextSiblingElement("object"))
		{
			HelpInfo* tempInfo = new HelpInfo();

			tempInfo->setName( objectEl->Attribute("name") );
			TiXmlElement* descpEl = objectEl->FirstChildElement("description");
			tempInfo->setDescription(descpEl->GetText());

			TiXmlElement* imageEl = objectEl->FirstChildElement("image");
			//Get the path to load the image from, load it into the texture manager, and store the ID.
			const char* imagePath = imageEl->GetText();
			if(imagePath != nullptr)
				tempInfo->setImageID( CSGD_TextureManager::GetInstance()->LoadTexture(imagePath) );

			infoVector.push_back(tempInfo);
		}
	}
}

//NOTE: names in XML MUST MATCH OBJECT IDS in levelmanager
void HelpInfoManager::addObject(string name)
{
	//Search through the object vector
	//	-If the object to be added is already in it, return.
	for(unsigned int i = 0; i < objectVector.size(); ++i)
	{
		if(objectVector[i]->getName() == name)
			return;
	}

	for(unsigned int curInfo = 0; curInfo < infoVector.size(); ++curInfo)
	{
		if(infoVector[curInfo]->getName() == name)
		{
			if(infoVector[curInfo] != nullptr && this != nullptr)
			{
				objectVector.push_back(infoVector[curInfo]);
				return;
			}
		}
	}
}
void  HelpInfoManager::clearObjects()
{
	objectVector.clear();
}
void HelpInfoManager::resetObjects()
{
	for(unsigned int objects = 0; objects < objectVector.size(); ++ objects)
	{
		objectVector[objects] = nullptr;
	}
	objectVector.clear();
	addObject("Player");
}